<?php
/**
 * Atom feed generator
 *
 * Generates a Atom feed from the available items.
 * Based on Wordpress' feed-atom.php
 *
 * @author Ryan McCue <cubegames@gmail.com>
 * @author WordPress Team
 * @package Lilina
 * @version 1.0
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

class Resyndicate {
	public static function atom() {
		header('Content-type: application/atom+xml; charset=' . get_option('encoding', 'utf-8'), true);
		echo '<?xml version="1.0" encoding="' . get_option('encoding', 'utf-8') . '"?'.'>'; ?>
<feed xmlns="http://www.w3.org/2005/Atom"
	xmlns:thr="http://purl.org/syndication/thread/1.0"
	xml:lang="<?php echo get_option('lang'); ?>"
	xml:base="<?php echo get_option('baseurl'); ?>atom.php"
	<?php do_action('atom_ns'); ?>>
	<title type="text"><?php echo get_option('sitename'); ?></title>

	<?php //Need to fix this ?>
	<updated><?php echo date('Y-m-d\TH:i:s\Z'); ?></updated>
	<generator uri="http://getlilina.org/" version="<?php echo LILINA_CORE_VERSION; ?>">Lilina News Aggregator</generator>

	<link rel="alternate" type="text/html" href="<?php echo get_option('baseurl'); ?>" />
	<id><?php echo get_option('baseurl');?>atom.php</id>
	<link rel="self" type="application/atom+xml" href="<?php echo get_option('baseurl'); ?>atom.php" />

	<?php do_action('atom_head'); ?>
	<?php
	//query_setup('showtime=0');
	global $showtime;
	$showtime = 0;
	if(has_items()) {
		while(has_items()): the_item();
	?>
	<entry>
		<author>
			<name><?php the_feed_name(); ?></name>
			<uri><?php the_feed_url(); ?></uri>
		</author>
		<title type="html"><![CDATA[<?php the_title(); ?>]]></title>
		<link rel="alternate" type="text/html" href="<?php the_link(); ?>" />
		<id><?php the_id(); ?></id>
		<updated><?php the_date('Y-m-d\TH:i:s\Z'); ?></updated>
		<published><?php the_date('Y-m-d\TH:i:s\Z'); ?></published>
		<summary type="html"><![CDATA[<?php the_summary(); ?>]]></summary>
		<content type="html" xml:base="<?php the_link(); ?>"><![CDATA[<?php the_summary(); ?>]]></content>
		<?php atom_enclosure(); ?>
		<?php do_action('atom_entry'); ?>
	</entry>
	<?php
		endwhile;
	}
	?>
</feed>
<?php
	}
	
	public static function rss2() {
		header('Content-Type: application/rss+xml; charset=' . get_option('encoding', 'utf-8'), true);
		echo '<?xml version="1.0" encoding="' . get_option('encoding', 'utf-8') . '"?'.'>';
?>

<!-- Generated by Lilina/<?php echo LILINA_CORE_VERSION; ?> -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	<?php do_action('rss2_ns'); ?>
>

<channel>
	<title><?php echo get_option('sitename');?></title>
	<link><?php echo get_option('baseurl') ?></link>
	<description><?php echo get_option('sitename'); ?></description>
	<atom:link href="<?php echo get_option('baseurl'); ?>rss.php" rel="self" type="application/rss+xml" />
	<?php //Need to fix this ?>
	<pubDate><?php echo date('D, d M Y H:i:s +0000'); ?></pubDate>
	<generator>http://getlilina.org/?v=<?php echo LILINA_CORE_VERSION; ?></generator>
	<language><?php echo get_option('lang'); ?></language>
	<?php do_action('rss2_head');
	if(has_items()) {
		while(has_items()) { the_item();
	?>
	<item>
		<title><![CDATA[<?php the_title(); ?>]]></title>
		<link><?php the_link(); ?></link>
		<pubDate><?php the_date(array('format' => 'D, d M Y H:i:s +0000')); ?></pubDate>
		<?php //Not entirely accurate; uses the feed name, not the author ?>
		<dc:creator><?php the_feed_name(); ?></dc:creator>

		<guid isPermaLink="false"><?php the_id(); ?></guid>
		<description><![CDATA[<?php the_summary(); ?>]]></description>
		<content:encoded><![CDATA[<?php the_content(); ?>]]></content:encoded>
		<?php do_action('rss2_item'); ?>
	</item>
	<?php }
	}
	?>
</channel>
</rss>
<?php
	}
	
	public static function error() {
		header('Content-Type: text/plain; charset=utf-8');
		_e('Whoops! Invalid feed type supplied.');
	}
	
	public static function dispatch() {
		$type = '';
		if(!empty($_GET['type']))
			$type = preg_replace('/[^-_.0-9a-zA-Z]/', '', $_GET['type']);
		
		switch($type) {
			case 'atom':
				self::atom();
				break;
			case 'rss2':
				self::rss2();
				break;
			default:
				self::error();
				break;
		}
	}
}

Controller::registerMethod('feed', array('Resyndicate', 'dispatch'));